import { defineStore } from "pinia";
import { ref } from 'vue';
import { getLogin } from '@/api/user';
import { useCartStore } from "./cart";
import { comixCartAPI } from "@/api/cart";
export const useUserStore = defineStore('user', () => {
    const userinfo = ref({})
    const getUserinfo = async ({ account, password }) => {
        const res = await getLogin({ account, password });
        userinfo.value = res.result
        //合并购物车
        const cartStore = useCartStore()
        await comixCartAPI(cartStore.cartList.map(item => {
            return {
                skuId: item.skuId,
                selected: item.selected,
                count: item.count
            }
        }))
        cartStore.updateCart()
    }
    const cleanUserinfo = () => {
        userinfo.value = {}
        const cartStore = useCartStore()
        cartStore.cleanCart()
    }
    return {
        userinfo,
        getUserinfo,
        cleanUserinfo
    }
},
    {
        persist: true
    }
)
